package com.cema.manage.project.android.biz;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.common.utils.DateUtils;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.project.enums.RiskEnum;
import com.cema.manage.project.manage.usersLogout.domain.UsersLogout;
import com.cema.manage.project.manage.usersLogout.service.IUsersLogoutService;
import com.cema.manage.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("config")
public class BizRedisController {
    @Resource
    private RedisService redisService;
    private static final Logger log = LoggerFactory.getLogger(BizRedisController.class);

    @Resource
    private IUsersLogoutService iUsersLogoutService;

    @PostMapping("info")
    public JsonResult config(HttpServletRequest request) {
        String uvChannel = request.getParameter("uvChannel");
        if (StrUtil.isEmpty(uvChannel)) {
            return JsonResult.returnResetLogin("登录失效、请重新登录");
        }
        Map<String, Object> map = new HashMap<>();
        map.put(RedisEnum.APP_ID.getKey(), redisService.get(RedisEnum.APP_ID.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_APP_ID.getKey(), redisService.get(RedisEnum.TA_KU_APP_ID.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_APP_KEY.getKey(), redisService.get(RedisEnum.TA_KU_APP_KEY.getKey() + uvChannel));
        map.put(RedisEnum.BANNER.getKey(), redisService.get(RedisEnum.BANNER.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_BANNER.getKey(), redisService.get(RedisEnum.TA_KU_BANNER.getKey() + uvChannel));
        map.put(RedisEnum.INCENTIVE_VIDEO_CALLBACK.getKey(), redisService.get(RedisEnum.INCENTIVE_VIDEO_CALLBACK.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_INCENTIVE_VIDEO_CALLBACK.getKey(), redisService.get(RedisEnum.TA_KU_INCENTIVE_VIDEO_CALLBACK.getKey() + uvChannel));
        map.put(RedisEnum.INCENTIVE_VIDEO.getKey(), redisService.get(RedisEnum.INCENTIVE_VIDEO.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_INCENTIVE_VIDEO.getKey(), redisService.get(RedisEnum.TA_KU_INCENTIVE_VIDEO.getKey() + uvChannel));
        map.put(RedisEnum.TIME_INTERVAL.getKey(), redisService.get(RedisEnum.TIME_INTERVAL.getKey() + uvChannel));
        map.put(RedisEnum.SPREAD_ITS_TAIL.getKey(), redisService.get(RedisEnum.SPREAD_ITS_TAIL.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_SPREAD_ITS_TAIL.getKey(), redisService.get(RedisEnum.TA_KU_SPREAD_ITS_TAIL.getKey() + uvChannel));
        map.put(RedisEnum.BANNER_TWO.getKey(), redisService.get(RedisEnum.BANNER_TWO.getKey() + uvChannel));
        map.put(RedisEnum.AD_INFORMATION_FLOW.getKey(), redisService.get(RedisEnum.AD_INFORMATION_FLOW.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_AD_INFORMATION_FLOW.getKey(), redisService.get(RedisEnum.TA_KU_AD_INFORMATION_FLOW.getKey() + uvChannel));
        map.put(RedisEnum.AD_INSERT_HALF_SCREEN.getKey(), redisService.get(RedisEnum.AD_INSERT_HALF_SCREEN.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_AD_INSERT_HALF_SCREEN.getKey(), redisService.get(RedisEnum.TA_KU_AD_INSERT_HALF_SCREEN.getKey() + uvChannel));
        map.put(RedisEnum.AD_INSERT_FULL_SCREEN.getKey(), redisService.get(RedisEnum.AD_INSERT_FULL_SCREEN.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU_AD_INSERT_FULL_SCREEN.getKey(), redisService.get(RedisEnum.TA_KU_AD_INSERT_FULL_SCREEN.getKey() + uvChannel));
        map.put(RedisEnum.DAILY_TASK_FREQUENCY.getKey(), redisService.get(RedisEnum.DAILY_TASK_FREQUENCY.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_ONE.getKey(), redisService.get(RedisEnum.SIGN_IN_ONE.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_TWO.getKey(), redisService.get(RedisEnum.SIGN_IN_TWO.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_THREE.getKey(), redisService.get(RedisEnum.SIGN_IN_THREE.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_FIVE.getKey(), redisService.get(RedisEnum.SIGN_IN_FIVE.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_FOUR.getKey(), redisService.get(RedisEnum.SIGN_IN_FOUR.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_SEVEN.getKey(), redisService.get(RedisEnum.SIGN_IN_SEVEN.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_SIX.getKey(), redisService.get(RedisEnum.SIGN_IN_SIX.getKey() + uvChannel));
        map.put(RedisEnum.SIGN_IN_NINE.getKey(), redisService.get(RedisEnum.SIGN_IN_NINE.getKey() + uvChannel));
        map.put(RedisEnum.MEDIATION_FULL_AD.getKey(), redisService.get(RedisEnum.MEDIATION_FULL_AD.getKey() + uvChannel));
        map.put(RedisEnum.PLAYLET_FREE_SET.getKey(), redisService.get(RedisEnum.PLAYLET_FREE_SET.getKey() + uvChannel));
        map.put(RedisEnum.WITHDRAWAL_NUMBER_SIGN.getKey(), redisService.get(RedisEnum.WITHDRAWAL_NUMBER_SIGN.getKey() + uvChannel));
        //APP 开屏广告
        map.put(RedisEnum.APP_SPREAD_ITS_TAIL.getKey(), redisService.get(RedisEnum.APP_SPREAD_ITS_TAIL.getKey() + uvChannel));
        //激励视频（答题）
        map.put(RedisEnum.APP_ANSWER_INCENTIVE.getKey(), redisService.get(RedisEnum.APP_ANSWER_INCENTIVE.getKey() + uvChannel));
        //激励视频 (签到)
        map.put(RedisEnum.APP_ANSWER_SIGN_IN.getKey(), redisService.get(RedisEnum.APP_ANSWER_SIGN_IN.getKey() + uvChannel));
        //激励视频（提现）
        map.put(RedisEnum.APP_ANSWER_WITHDRAWAL.getKey(), redisService.get(RedisEnum.APP_ANSWER_WITHDRAWAL.getKey() + uvChannel));
        //全屏广告（副广）
        map.put(RedisEnum.APP_FULL_SCREEN.getKey(), redisService.get(RedisEnum.APP_FULL_SCREEN.getKey() + uvChannel));
        //信息流（答错）
        map.put(RedisEnum.APP_FLOW_ANSWER_INCORRECTLY.getKey(), redisService.get(RedisEnum.APP_FLOW_ANSWER_INCORRECTLY.getKey() + uvChannel));
        //信息流（答对）
        map.put(RedisEnum.APP_FLOW_ANSWER.getKey(), redisService.get(RedisEnum.APP_FLOW_ANSWER.getKey() + uvChannel));
        //信息流（奖励）
        map.put(RedisEnum.APP_FLOW_REWARD.getKey(), redisService.get(RedisEnum.APP_FLOW_REWARD.getKey() + uvChannel));
        //信息流（提现）
        map.put(RedisEnum.APP_FLOW_WITHDRAWAL.getKey(), redisService.get(RedisEnum.APP_FLOW_WITHDRAWAL.getKey() + uvChannel));
        //信息流（我的）
        map.put(RedisEnum.APP_FLOW_MY.getKey(), redisService.get(RedisEnum.APP_FLOW_MY.getKey() + uvChannel));
        //插屏（提现）
        map.put(RedisEnum.APP_TABLE_PLAQUE_WITHDRAWAL.getKey(), redisService.get(RedisEnum.APP_TABLE_PLAQUE_WITHDRAWAL.getKey() + uvChannel));
        //APP 开屏广告
        map.put(RedisEnum.APP_BANNER.getKey(), redisService.get(RedisEnum.APP_BANNER.getKey() + uvChannel));
        //插屏 (金/红记录)
        map.put(RedisEnum.APP_TABLE_PLAQUE_RECORD.getKey(), redisService.get(RedisEnum.APP_TABLE_PLAQUE_RECORD.getKey() + uvChannel));
        //QQ交流群
        Object qqObject = redisService.get(RedisEnum.APP_QQ_COMMUNICATION_GROUP.getKey() + uvChannel);
        map.put(RedisEnum.APP_QQ_COMMUNICATION_GROUP.getKey(), ObjectUtil.isNotEmpty(qqObject) ? qqObject : "");
        //微信客服
        Object qqServiceObject = redisService.get(RedisEnum.APP_QQ_CUSTOMER_SERVICE.getKey() + uvChannel);
        map.put(RedisEnum.APP_QQ_CUSTOMER_SERVICE.getKey(), ObjectUtil.isNotEmpty(qqServiceObject) ? qqServiceObject : "");
        //开发者模式（APP拦截）
        map.put(RedisEnum.RISK_DEVELOPER_MODE_APP.getKey(), redisService.get(RedisEnum.RISK_DEVELOPER_MODE_APP.getKey() + uvChannel));
        //USB调试模式（APP拦截）
        map.put(RedisEnum.RISK_DEBUG_MODE_APP.getKey(), redisService.get(RedisEnum.RISK_DEBUG_MODE_APP.getKey() + uvChannel));
        //root模式（APP拦截）
        map.put(RedisEnum.RISK_ROOT_APP.getKey(), redisService.get(RedisEnum.RISK_ROOT_APP.getKey() + uvChannel));
        //无障碍模式（APP拦截）
        map.put(RedisEnum.RISK_ACCESSIBILITY_APP.getKey(), redisService.get(RedisEnum.RISK_ACCESSIBILITY_APP.getKey() + uvChannel));
        //IP代理模式（APP拦截）
        map.put(RedisEnum.RISK_ID_PROXY_APP.getKey(), redisService.get(RedisEnum.RISK_ID_PROXY_APP.getKey() + uvChannel));
        //双开（APP拦截）
        map.put(RedisEnum.RISK_DOUBLE_OPEN_APP.getKey(), redisService.get(RedisEnum.RISK_DOUBLE_OPEN_APP.getKey() + uvChannel));
        //模拟器（APP拦截）
        map.put(RedisEnum.RISK_SIMULATOR_APP.getKey(), redisService.get(RedisEnum.RISK_SIMULATOR_APP.getKey() + uvChannel));
        map.put(RedisEnum.TA_KU.getKey(), redisService.get(RedisEnum.TA_KU.getKey() + uvChannel));
        map.put(RedisEnum.GRO_MORE.getKey(), redisService.get(RedisEnum.GRO_MORE.getKey() + uvChannel));
        //小视频
        map.put(RedisEnum.GM_VIDEO.getKey(), redisService.get(RedisEnum.GM_VIDEO.getKey() + uvChannel));
        //短剧
        map.put(RedisEnum.GM_SHORT_PLAY.getKey(), redisService.get(RedisEnum.GM_SHORT_PLAY.getKey() + uvChannel));
        //答题获取奖励（等待时间/毫秒）
        map.put(RedisEnum.TIME_INTERVAL_ANSWER.getKey(), redisService.get(RedisEnum.TIME_INTERVAL_ANSWER.getKey() + uvChannel));
        //答题错误（等待时间/毫秒）
        map.put(RedisEnum.TIME_INTERVAL_ANSWER_ERROR.getKey(), redisService.get(RedisEnum.TIME_INTERVAL_ANSWER_ERROR.getKey() + uvChannel));
        //答题领取奖励（等待时间/毫秒）
        map.put(RedisEnum.TIME_INTERVAL_ANSWER_RECEIVE.getKey(), redisService.get(RedisEnum.TIME_INTERVAL_ANSWER_RECEIVE.getKey() + uvChannel));
        map.put(RedisEnum.WX_LOGIN_APP_ID.getKey(), redisService.get(RedisEnum.WX_LOGIN_APP_ID.getKey() + uvChannel));
        map.put(RedisEnum.WX_LOGIN_APP_SECRET.getKey(), redisService.get(RedisEnum.WX_LOGIN_APP_SECRET.getKey() + uvChannel));
        String eight = (String) redisService.get(RedisEnum.SIGN_IN_EIGHT.getKey() + uvChannel);
        String[] eights = eight.split(",");
        if (eights.length == 2) {
            int eightCount = RandomUtil.randomInt(Integer.parseInt(eights[0]), Integer.parseInt(eights[1]));
            map.put(RedisEnum.SIGN_IN_EIGHT.getKey(), eightCount);
        }

        //是否注销用户 返回注销时间
        EntityWrapper<UsersLogout> usersLogoutEntityWrapper = new EntityWrapper<>();
        String deviceId = request.getParameter("info");
        usersLogoutEntityWrapper.eq("ul_device_id", deviceId);
        UsersLogout usersLogout = iUsersLogoutService.selectOne(usersLogoutEntityWrapper);
        String time = "";
        if (ObjectUtil.isNotNull(usersLogout)) {
            time = DateUtil.format(usersLogout.getCreateTime(), DateUtils.YEAR_MONTH_DAY);
        }
        map.put("logoutTime", time);
        return JsonResult.returnResult(JsonResult.SUCCESS, map);
    }
}
